import {mapGetters} from "vuex";
import * as ProductApi from '@/api/school/product.js';
import diyJson from '@/config/diy.js';

export const mixins =
    {
      computed: mapGetters(['uid', 'userInfo', 'shareInfo', 'schoolId']),
      data() {
        return {
          themConfig: diyJson.commonStyle,
          product: {
            id: '',
            description: "",
            productNo: "",
            productName: "",
            price: 0,
            typeNo: "",
            hamlet: '',
            userPhone: "",
            images: [],
            wechatId: "",
            typeName: "",
          },
          typeName: '未选择',
          productTypes: []
        }
      },

      onLoad: function (options) {
        let productNo = options.productNo;
        if (productNo){
          this.product.productNo = productNo;
          ProductApi.editDetail(productNo).then(res=>{
            this.product = res.data
          })
        }
      }
      ,
      onShow() {
        if (this.userInfo.schoolInfo) {
          this.product.schoolId = this.userInfo.schoolInfo.schoolId;
          this.product.schoolName = this.userInfo.schoolInfo.schoolName;
        }else {
          uni.showToast({
            title: '需要先注册完善学校学校哦～',
            duration: 4500,
            icon: 'none'
          });
          setTimeout(()=>{
            uni.navigateBack();
          },2000)
          return;
        }
        this.product.userPhone = this.userInfo.phone
        let that = this;
        ProductApi.typeList().then(res => {
          this.productTypes = res.data;

         setTimeout(()=>{
           if (that.product.typeNo){
             for (let i = 0; i < res.data.length; i++) {
               if (res.data[i].value === this.product.typeNo){
                 that.product.typeName = res.data[i].name;
                 that.product = {...that.product}
                 break;
               }
             }
           }
         },500)

        })
      },

      methods: {
        uploadSuccess(array) {
          console.log("array", array)
          let photos = [];
          for (let i = 0; i < array.images.length; i++) {
            photos.push(array.images[i].url);
          }
          this.product.images = photos;
          console.log("form", this.product)
        },
        // 输入金额变化
        amountInputChange(e) {
          this.$nextTick(() => {
            this.product.price = parseFloat(e).toFixed(2);
          });
          console.log("amount", this.product)
        },

        // 选择常用地址
        selectProductType() {
          this.$refs.picker.open();
        },
        productTypeConfirm(e) {
          this.product.typeNo = e.value[0].value;
          this.product.typeName = e.value[0].name;
        },

        saveProduct() {
          ProductApi.addProduct(this.product).then(res=>{
            if (res.code===200){

              uni.showToast({
                title: '发布成功',
                duration: 2000,
                icon: 'none'
              });
              setTimeout(()=>{
                uni.navigateBack();
              },1000)
            }
          })
        },

        updateProduct() {
          ProductApi.updateProduct(this.product).then(res=>{
            if (res.code===200){

              uni.showToast({
                title: '更新成功',
                duration: 2000,
                icon: 'none'
              });
              setTimeout(()=>{
                uni.navigateBack();
              },1000)
            }
          })
        },

        submit() {
          console.log("form",this.product)
          if (!this.product.description){
            uni.showToast({
              title: '请填写物品描述',
              icon: 'none',
              duration: 2000
            });
            return;
          }

          if (!this.product.images || this.product.images<=0){
            uni.showToast({
              title: '请上传物品图',
              icon: 'none',
              duration: 2000
            });
            return;
          }

          if (!this.product.productName){
            uni.showToast({
              title: '请填写物品名称',
              icon: 'none',
              duration: 2000
            });
            return;
          }

          if (!this.product.description){
            uni.showToast({
              title: '请填写物品描述',
              icon: 'none',
              duration: 2000
            });
            return;
          }

          if (!this.product.typeNo){
            uni.showToast({
              title: '请填写物品类型',
              icon: 'none',
              duration: 2000
            });
            return;
          }
          if (this.product.id){
            this.updateProduct();
          }else {
            this.saveProduct();
          }
        }

      },

      onReachBottom() {

      },
      onPullDownRefresh() {
      },
    }
